package cn.fy.location.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import cn.fy.location.databinding.ActivityLocationBinding;
import cn.fy.location.databinding.ActivityMainBinding;

public class LocationActivity extends AppCompatActivity {

    private ActivityLocationBinding binding;

    private LocationManager lm;
    private List<String> pNames = new ArrayList<String>(); // 存放LocationProvider名称的集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_location);
        binding = ActivityLocationBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("定位");

        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Button btnLocation1 = binding.btnLocation1;
        btnLocation1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pNames.clear();
                pNames = lm.getAllProviders();
                binding.tvResult.setText(getProvider());
            }
        });

        Button btnLocation2 = binding.btnLocation2;
        btnLocation2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pNames.clear();
                Criteria criteria = new Criteria();
                criteria.setCostAllowed(false);   //免费
                criteria.setAltitudeRequired(true);  //能够提供高度信息
                criteria.setBearingRequired(true);   //能够提供方向信息
                pNames = lm.getProviders(criteria, true);
                binding.tvResult.setText(getProvider());
            }
        });

        Button btnLocation3 = binding.btnLocation3;
        btnLocation3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pNames.clear();
                pNames.add(lm.getProvider(LocationManager.GPS_PROVIDER).getName()); //指定名称
                binding.tvResult.setText(getProvider());
            }
        });

        Button btnLocation4 = binding.btnLocation4;
        btnLocation4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ActivityCompat.checkSelfPermission(LocationActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(LocationActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                lm.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,//指定GPS定位的提供者
                        1000,//间隔时间
                        1,//位置更新之间的最小距离
                        new LocationListener() { //监听GPS定位信息是否改变
                            @Override
                            public void onLocationChanged(@NonNull Location location) { //GPS信息发生改变时回调
                                locationUpdates(location);
                            }
                        }
                );
            }
        });

        Button btnLocation5 = binding.btnLocation5;
        btnLocation5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ActivityCompat.checkSelfPermission(LocationActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(LocationActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                //获取最新的定位信息
                Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                //将最新的定位信息，传递给LocationUpdates()方法
                locationUpdates(location);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //遍历数组返回字符串的方法
    private String getProvider(){
        StringBuilder sb = new StringBuilder();
        for (String s : pNames) {
            sb.append(s + "\n");
        }
        return sb.toString();
    }

    public void locationUpdates(Location location) {
        if (null != location) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("您的位置是：\n");
            stringBuilder.append("经度：");
            stringBuilder.append(location.getLongitude());
            stringBuilder.append("\n纬度：");
            stringBuilder.append(location.getLatitude());
            binding.tvResult.setText(stringBuilder.toString());
        } else {
            binding.tvResult.setText("没有获取到GPS信息");
        }
    }
}